#!/usr/bin/env bash

#-------------------------------------------------
#   File Name：     ${0}
#   Description :
#  Author :       Zampuser
#   date：          2020/7/10
#   version:        1.0.0
#-------------------------------------------------
#   Change Activity:
#                   2020/7/10:
#-------------------------------------------------


work_date=$(date +%Y%m%d)
work_time=$(date +%H%M%S)
work_dir=$(cd "$(dirname "$0")";pwd)
cd "${work_dir}"

function echo_c(){
    action_code=1
    case $1 in
        y)
            echo_log "\033[33;1m" "${*:2:$#}" "\033[0m"
            ;;
        r)
            echo_log "\e[31;1m" "${*:2:$#}" "\e[0m"
            ;;
        *)
            echo_log "\E[32;1m" "${*:2:$#}" "\E[0m"
        ;;
    esac
}

function echo_log(){
    if [[ ${action_code} -eq 0 ]];then
        echo -e "[$(date +%Y%m%d_%H:%M:%S) $$] $2" | tee -a $0.log
    else
        echo -e "$1[$(date +%Y%m%d_%H:%M:%S.%s) $$] $2 $3" | tee -a $0.log
    fi
}

echo_title(){
    # pycharm模板，使用时需要去除#和{}的转义符
    text="${*}"
    line_max_len=60
    line_text_max_len=52
    char_echo="#"
    line=$(seq -s ${char_echo} ${line_max_len}|sed -E 's/[0-9]//g')
    echo_c y ""
    echo_c y ${line}

    text_len=${#text}
    start_line_count=0
    if [[ "${text_len}" -gt "${line_text_max_len}" ]];then
        line_num=$(expr ${text_len} / ${line_text_max_len})
        line_text_max_end=${line_text_max_len}
        for n in $(seq 1 +1 ${line_num});do
            texts="$\{text:$start_line_count:$line_text_max_end\}"
            if [[ $n -eq 1 ]];then
                echo_c y "\#\#  ${texts} \ "
            else
                echo_c y "\#\#\t${texts} \ "
            fi
            start_line_count=$line_text_max_end
            line_text_max_end=$line_text_max_len
        done

        last_line=$(expr ${text_len} % ${line_text_max_len})
        if [[ $last_line -ne 0 ]];then
            echo_c y "\#\#\t$\{text:0-$last_line\}"
        fi
    else
        echo_c y "\#\#  $\{text\}"
    fi

    echo_c y ${line}
}

check_error(){
    if [[ $1 -ne 0 ]];then
        echo_c r "$2:$3"
        echo "$2:$3"
        exit 1
    fi
}

# check lock file, one time only let the script run one time
check_lockfile(){
    LOCKfile=/tmp/.$(basename $0)
    case $1 in
    start)
        if [ -f "$LOCKfile" ];then
            check_error 1 1 "The script is already exist, please next time to run this script"
        else
          echo_c g "No lock file, begin to create lock file and continue"
          touch $LOCKfile
        fi
        ;;
    stop)
        if [ -f "$LOCKfile" ];then
            rm -rf "$LOCKfile"
        else
            echo_c y "No lock file, exit..."
        fi
        ;;
    *)
        check_error 1 1 "[check_lockfile] USEAGE: check_lockfile [ start | stop ]"
        ;;
    esac
}

check_node(){
    if [[ $(echo ${1::1}) == "#" ]];then
        return 1
    else
        return 0
    fi
}

check_lockfile 'start'
trap "check_lockfile stop " EXIT TSTP TERM HUP

run_user='root'
[ $(id -aun) != ${run_user} ] && { check_error 9 1 "Error: You must be ${run_user} to run this script"; exit 1; }

echo_c g "开始运行脚本..."


#================================ 配置 ================================

#salt -N 'pdapiGroup' state.sls prod.item_paidan_api_files
salt 'host-172-36-12-57' state.sls prod.item_paidan_api_files runas=amos
#salt 'cos75-150-7' state.sls prod.item_paidan_api_files
